// components/SearchList/searchList.js
import { request,canDownload } from '../../utils/util'
const { $Message } = require('../../dist/base/index');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    type: {
      type: String
    },
    name: {
      type: String
    },
    size: {
      type: String
    },
    time: {
      type: String
    },
    uuid: {
      type: String
    },
    highlight: {
      type: Object
    },
    abstract: {
      type: String
    },
    uploadMan: {
      type: String
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    startTime: 0,
    endTime: 0,
    visibleOptions: false,
    actions: [//企业里已收藏的
      {
        name: "下载"
      },
    ],
  },
  /**
   * 组件的方法列表
   */
  methods: {

    handlePriview() {
      if (this.data.endTime - this.data.startTime < 350) {
        const noPreview = wx.getStorageSync('no_preview')
        let isNoPreview = false
        noPreview.forEach(item => {
          if (this.properties.type === item.name) {
            isNoPreview = true
            wx.showToast({
              title: '该类型不支持预览',
              icon: 'none'
            })
            return
          }
        })
        if (!isNoPreview) {
          const param = {
            file_hash: this.properties.uuid
          }
          request("POST", "/api/upload/doc/preview/", param, "", true)
            .then(res => {
              if (res.statusCode === 200) {
                wx.navigateTo({
                  url: `/pages/webView/preview/preview?url=${encodeURIComponent(res.data.url)}`,
                })
              }
            })
        }
      }


    },
    bindTouchStart: function (e) {
      this.setData({
        startTime: e.timeStamp
      })
      // this.startTime = e.timeStamp;
    },
    bindTouchEnd: function (e) {
      this.setData({
        endTime: e.timeStamp
      })
    },
    handleLongTap() {
      this.setData({
        ...this.data,
        visibleOptions: true
      })
    },
    handleClickOptions({ detail }) {
      const index = detail.index + 1
      if(index == 1){
        const isAllowDownload = canDownload(this.properties.type)
        const fileName = this.properties.name
        const allowImage = ["png","jeg","jpeg"]
        const type = this.properties.type
        this.handleCancel1();
            if (isAllowDownload) {
              const param = {
                file_hash: this.properties.uuid,
                type:'download'
              }
              request("POST", "/api/upload/doc/download/", param)
                .then(res => {
                  if (res.statusCode === 200) {
                    $Message({
                      content: '开始下载',
                    })
                    const downloadTask = wx.downloadFile({
                      url: res.data.url, 
                      filePath:wx.env.USER_DATA_PATH + "/" + fileName,
                      success(res) {
                       
                        if(allowImage.indexOf(type) !== -1){
                          wx.previewImage({
                            urls:[res.filePath],
                            showMenu:true,
                            success: function (res) {
                              console.log('打开文档成功')
                            },
                            fail: res => {
                              console.log(res)
                            }
                          })
                        }else{
                          wx.openDocument({
                            filePath: res.filePath,
                            showMenu: true,
                            fileType: type,
                            success: function (res) {
                              console.log('打开文档成功')
                            },
                            fail: res => {
                              console.log(res)
                            }
                          })
                        }
                        

                      },
                      fail: res => {
                        console.log(res)
                      }

                    })
                    downloadTask.onProgressUpdate((res) => {
                      $Message({
                        content: '正在下载' + res.progress + '%',
                      })
                      if (res.progress === 100) {
                        $Message({
                          content: '下载成功,正在打开',
                          type: 'success',

                        })
                      }
                    })

                  } else if (res.statusCode === 430) {
                    wx.showToast({
                      title: res.data.detail,
                      icon: "none"
                    })
                  } else {
                    wx.showToast({
                      title: '服务器异常，请联系系统管理员',
                      icon: 'none'
                    })
                  }
                })
            } else {
              wx.showToast({
                title: '该文件类型暂不支持下载，请至web端进行下载',
                icon: 'none',
                duration: 1500
              })
            }
      }
    },
    handleCancel1() {
      this.setData({
        visibleOptions: false
      })
    }
  }
})
